package com.example.springmvcdemo3;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RequestMapping("/resp")
@Controller//返回视图   暂时理解为该类被spring管理
//@RestController   @Controller（返回视图）+@ResponseBody（返回数据）
public class ResponseController {
    @RequestMapping("/index")
    public String index(){
        return "/index.html";
    }
    @RequestMapping("/index2")
    @ResponseBody
    public String index2(){
        return "返回数据";
    }
    //返回html
    @RequestMapping("/index3")
    @ResponseBody
    public String index3(){
        return "<h1>hello springmvc<h1>";
    }
    //返回json
    @RequestMapping("/index4")
    @ResponseBody
    public HashMap<String,String> index4(){
        HashMap<String,String> map = new HashMap<>();
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");
        map.put("k4","v4");
        return map;
    }
    //设置状态码
    @ResponseBody
    @RequestMapping("/index5")
    public String setStatus(HttpServletResponse response){
        response.setStatus(418);
        return "设置状态码";
    }
    //设置返回类型
    @RequestMapping(value = "return",produces = "applicatin/json")
    @ResponseBody
    public String returnJson(){
        return "{\"success\":true}";
    }

}
